package com.mengchengnet.chaperonage.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;
import com.mengchengnet.chaperonage.view.RatingBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/7.
 */

public class RatingActivity extends BaseActivity {
    private RatingBar ratingBar;
    private TextView textView;
    private EditText editText;
    private Button button;
    private String level="5.0";
    private RequestQueue requestQueue;
    private static final String TAG = "RatingActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rating);
        toolbarState=TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("发表评价");
        requestQueue= Volley.newRequestQueue(getApplication());
        ratingBar= (RatingBar) findViewById(R.id.rating_star);
        textView= (TextView) findViewById(R.id.rating_tip);
        editText= (EditText) findViewById(R.id.rating_ed);
        button= (Button) findViewById(R.id.rating_btn);
        ratingBar.setStar(5);
        textView.setText("非常满意");
        ratingBar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float RatingCount) {
                level=Float.toString(RatingCount);
                if(RatingCount==5.0){
                    textView.setText("非常满意");
                }else if(RatingCount==4.0){
                    textView.setText("满意");
                }else if(RatingCount==3.0){
                    textView.setText("一般");
                }else if(RatingCount==2.0){
                    textView.setText("差");
                }else if(RatingCount==1.0){
                    textView.setText("非常差");
                }

            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!editText.getText().toString().isEmpty()){
                    upLoad();
                }else{
                    Toast.makeText(RatingActivity.this, "评价内容不能为空！", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void upLoad(){
        StringRequest request = new StringRequest(Request.Method.POST, Url.serviceComment,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e("上传评价", "上传评价接口返回值："+result );
                        JSONObject jsonObject;
                        try {
                            if(result != null && result.startsWith("\ufeff"))
                            {
                                result =  result.substring(1);
                            }
                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                Toast.makeText(RatingActivity.this, "评价成功！", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                            else {
                                Toast.makeText(RatingActivity.this, "失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Toast.makeText(RatingActivity.this, arg0.toString(), Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("version", PreferenceUtil.getString(getApplication(),"versionCode"));
                map.put("zescort_id", getIntent().getStringExtra("careId"));
                map.put("loginId", PreferenceUtil.getString(getApplication(),"userId"));
                map.put("level", level);
                map.put("content", editText.getText().toString());
                Log.e(TAG, "陪护id: "+getIntent().getStringExtra("careId") );
                Log.e(TAG, "loginId: "+PreferenceUtil.getString(getApplication(),"userId"));
                Log.e(TAG, "陪护level:"+level);
                Log.e(TAG, "评价内容:"+editText.getText().toString());
                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }
}
